package cn.kmust.project01.controller;

import cn.kmust.project01.entity.Notice;
import cn.kmust.project01.mapper.NoticeMapper;
import cn.kmust.project01.service.NoticeServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
public class NoticeController {
    @Autowired
    private NoticeServiceImp noticeServiceImp;

    @GetMapping("/allNotice")
    public List<Notice> queryAll(){
        List<Notice> NoticeList = noticeServiceImp.queryAll();
        for (Notice notice : NoticeList) {
            System.out.println(notice);
        }
        return NoticeList;
    }
    @GetMapping("/notice/{id}")
    public Notice queryById(@PathVariable("id") Integer id, Model model){
        Notice notice = noticeServiceImp.queryById(id);
        model.addAttribute("notice",notice);
        return notice;
    }

    @PostMapping("/noticeAdd")
    public String addNotice(Notice notice){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String now = LocalDateTime.now().format(dateTimeFormatter);
        noticeServiceImp.addNotice(notice);
        return "OK !";
    }
}
